#include "CChatBox.h"

#include "CGameManager.h"

CChatBox::CChatBox(CGameManager *mgr) : mgr(mgr)
{
  env = mgr->getGUIEnv();

  //! Use Tab with Background instead
  base = env->addTab(rect<s32> (0, 400, 580, 550));
  //base->setImage(mgr->getDriver()->getTexture("chatbox.png"));
  //base->setScaleImage(true);
  //base->setUseAlphaChannel(true);

  chatOutput = env->addListBox(rect<s32>(10, 10, 570, 120), base);
  wchar_t chat[]= L"";

  //! TODO: Use a global ID for this
  chatInput = env->addEditBox(chat, rect<s32>(10, 120, 570, 145), true, base, 11);

  setVisible(false);
}

CChatBox::~CChatBox()
{
  base->remove();
}

void CChatBox::setVisible(bool visible)
{
  // Show the GUI elements
  if ( visible )
  {
    base->setVisible(true);

    // Move the tab to front
    env->getRootGUIElement()->bringToFront(base);
  }
  // Hide the GUI elements
  else
    base->setVisible(false);
}

void CChatBox::addText(const wchar_t* text, bool fromServer)
{
  u32 id = chatOutput->addItem(text);

  chatOutput->setSelected(id);

  if ( fromServer )
    chatOutput->setItemOverrideColor (id, SColor(255, 0, 255, 255));
}

stringw CChatBox::getText()
{
  stringw text = chatInput->getText();
  chatInput->setText(L"");
  return text;
}
